package com.atguigu.luachangeUTF8;

import java.io.*;
import java.net.URLEncoder;
import java.util.Scanner;

/*
{E5B08FE5AE87E7A0B4E8A7A3} 先两两分开，在转换10进制
{E5,B0,8F,E5,AE,87,E7,A0,B4,E8,A7,A3} 转成 十进制

优化 中文转换

 */
public class demo01jinzhi {
    public static void main(String[] args) throws IOException {



        showMeu();


      //  showChoose_one();


        /*

       String    string = "{229,165,189,108,111,99,97,228,189,160,108,32,85,84,70,56}";
        //String s = "E4B88AE6B5B7";
       // System.out.println(convertUTF8ToString(s));

        englishToString(string);


*/









/*
        String name = "小宇破解";



        Zi zi = new Zi();
        String a =  zi.method(name);

      //  zhongwenzhuan(a);
         englishZhuan("local UTF8 = \"\"  for i = 1,#ghubcnm do UTF8 = UTF8 .. string.char(ghubcnm[i])  end ");
*/

    }


    public static String convertUTF8ToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }

        try {
            s = s.toUpperCase();

            int total = s.length() / 2;
            int pos = 0;

            byte[] buffer = new byte[total];
            for (int i = 0; i < total; i++) {

                int start = i * 2;

                buffer[i] = (byte) Integer.parseInt(
                        s.substring(start, start + 2), 16);
                pos++;
            }

            return new String(buffer, 0, pos, "UTF-8");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return s;
    }


    public static void englishToString(String string) throws UnsupportedEncodingException {
        String substring = string.substring(1, string.length() - 1);
        //108,111,99,97,108,32,85,84,70,56


        //你好的 UTF-8编码是E4BDA0E5A5BD，把每2个字符换算成10进制就是228,189,160,229,165,189
        StringBuffer sbu = new StringBuffer();
        String[] split = substring.split(",");
        for (int i = 0; i < split.length; i++) {
            split[i] = split[i].replace(" ", "");
        }

        for (int i = 0; i < split.length; ) {

            if (Integer.parseInt(split[i]) >=0 && Integer.parseInt(split[i]) <= 130)
            {

                sbu.append((char)  Integer.parseInt(split[i].replace(" ", "")))  ;
                i++;
            }
            else
                if (i + 3 <= split.length){

                String str1= convertUTF8ToString(Integer.toHexString(Integer.parseInt(split[i].trim())) +
                        Integer.toHexString(Integer.parseInt(split[i + 1].trim())) +
                        Integer.toHexString(Integer.parseInt(split[i + 2].trim())));


                sbu.append(str1);
                i += 3;


            }
                else
                    sbu.append((char)  Integer.parseInt(split[i].replace(" ", "")))  ;

        }
        System.out.println(sbu);

    }






    public  static int meuChoose(){
        Scanner sc = new Scanner(System.in);

        System.out.println("输入功能数字，别带.");
        int num = sc.nextInt();
        while (true){
            if ( num == 1){

                return  1;
            }
            else if ( num == 2){

                return  2;
            }
            else if ( num == 3){

                return  3;
            }


            else {
                System.out.println("你在输尼玛呢");
                continue;
            }

        }
    }

    public static void showChoose_one() throws IOException {
        System.out.println("=======加密页面=======");
        System.out.println("1.英文");
        System.out.println("2.中文");
        System.out.println("3.file");
        System.out.println("=====================");

       int num =  meuChoose();
       if (num == 1){
           System.out.println("输入【英文】 字符 按回车结束");
           String string = new Scanner(System.in).nextLine();
           englishZhuan(string);
       }

       else if(num == 2){
           System.out.println("输入【中文】  按回车结束");
           String string = new Scanner(System.in).nextLine();

           String a =  UTF8TOChineseMethod(string);


           zhongwenzhuan(a);
       }
       else if (num == 3){

           FileInputStream fin = new FileInputStream("1.txt");
           FileOutputStream out = new FileOutputStream("1toUTF.txt");
            int len = 0;
           byte[] bytes = new byte[100000];

           while (((len = fin.read(bytes)) != -1)) {


               String s = new String(bytes, 0, len);

              // System.out.println(s);
               String s1 = englishZhuanToFile(s);


               out.write(s1.getBytes());


           }


           out.close();
           fin.close();

       }
       else
           System.out.println("输错了" +
                   "");



    }


    public static String UTF8TOChineseMethod(String name){
        String sr = null;
        try {
            sr = URLEncoder.encode(name,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

//
        String[] split = sr.split("%");
        StringBuffer str5 = new StringBuffer();
        for (String s : split) {
            str5.append(s);
        }
        String string = str5.toString();
//

        return string;
    }




    public static void showChoose_two() throws UnsupportedEncodingException {
        System.out.println("=======解密页面=======");
        System.out.println("1.输入字符串 格式{97,97，97}");

        String s = new Scanner(System.in).nextLine();
        englishToString(s);
        System.out.println("=====================");

    }


    public static void showMeu() throws IOException {

      while (true){
          System.out.println();
          System.out.println("=========主菜单========");
          System.out.println("1.加密");
          System.out.println("2.解密");
          System.out.println("=====================");
          int i = meuChoose();
          if (i == 1){
              showChoose_one();
          }
          else
              showChoose_two();
      }

    }









    public static String englishZhuanToFile( String a){
//   97 32 97 97 97 97
        char[] chars = a.toCharArray();
        // System.out.println(chars.length);
        String  str = "{";
        for (int i = 0; i < chars.length; i++) {

            if ( i == 0){

                System.out.print("{" + (int)chars[i] + "," );
            }else if ( i == chars.length -1 ){
                str = str + (int)chars[i] + "}";
                System.out.print( (int)chars[i] + "}");
            }
            else{
                str = str + (int)chars[i] + ",";
                System.out.print((int)chars[i] + ","  );
            }

        }

        System.out.println();


        return str;
    }







    public static void englishZhuan( String a){
//   97 32 97 97 97 97
        char[] chars = a.toCharArray();
       // System.out.println(chars.length);
        for (int i = 0; i < chars.length; i++) {

                  if ( i == 0){
                      System.out.print("{" + (int)chars[i] + "," );
                  }else if ( i == chars.length -1 ){
                      System.out.print( (int)chars[i] + "}");
                  }
                  else
                      System.out.print((int)chars[i] + ","  );
            }

        System.out.println();
        }












public static void zhongwenzhuan( String a){
    char[] chars = a.toCharArray();
    String[] strings = new String[a.length()];
    for (int i = 0; i < chars.length ; i++) {
        if (i % 2 != 0 && i != chars.length -1){
            String a1 = chars[i] + ",";
            strings[i] = a1;
            System.out.print(a1);
        }
        else{
            String a2 = chars[i] + "";
            strings[i] = a2;
            System.out.print(a2);
        }

    }
    System.out.println();
    zhuanhuan(strings);
}



    public  static void zhuanhuan(String[] a){

        StringBuffer str5 = new StringBuffer();
        for (String s : a) {
            str5.append(s);
        }
        String string = str5.toString();

        String[] split = string.split(",");

        for (int i = 0; i < split.length; i++) {

            if ( i == 0 ){
                int y1= Integer.valueOf(split[i],16);
                System.out.print( "{" + y1 + ",");
            }else if ( i == split.length -1){
                int y1= Integer.valueOf(split[i],16);
                System.out.print( y1 + "}");
            }
            else{
                int y1= Integer.valueOf(split[i],16);
                System.out.print(y1 + ",");
            }


        }






    }
}
